package mp.weixin.vo;

import common.zsxmlv.utils.HttpUtils;
import common.zsxmlv.utils.PropertiesUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: QIDI
 * @Date: Create by 2018/7/19 22:30
 * @Version: 1.0
 * @Modify by:
 */
public class MpPush {

    /**
     * 获取 access_token
     * 返回AccessToken jsonObject 自己解析
     * @return {"access_token":"ACCESS_TOKEN","expires_in":7200}
     */
    public static String getAccessToken() {
        try {
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
            url = url.replaceAll("APPID", PropertiesUtil.getValue("auto_order.properties", "shangAppId"))
                    .replaceAll("APPSECRET", PropertiesUtil.getValue("auto_order.properties", "shangAppSecret"));
            String s = HttpUtils.get(url);
            JSONObject jsonObject = JSONObject.fromObject(s);
            return jsonObject.get("access_token").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取用户列表
     * @param accessToken
     */
    public static String getUserList(String accessToken) throws Exception {
            String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
            url = url.replaceAll("ACCESS_TOKEN", accessToken).replaceAll("NEXT_OPENID", "");
            return HttpUtils.get(url);
//            JSONObject jsonObject = JSONObject.fromObject(s);
//            String data = jsonObject.get("data").toString();
//            JSONObject jsonObject1 = JSONObject.fromObject(data);
//            String openid = jsonObject1.get("openid").toString();
//            JSONArray jsonArray = JSONArray.fromObject(openid);
//            for (int i = 0; i < jsonArray.size(); i++) {
//                openid = jsonArray.get(i).toString();
//                System.out.println(openid);
//            }

    }

    /**
     * 获取用户基本信息
     * @param openid
     */
    public static String getUserInfo(String accessToken, String openid) throws Exception {
//        try {
            String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
            url = url.replaceAll("ACCESS_TOKEN", accessToken).replaceAll("OPENID", openid);
            return HttpUtils.get(url);
//            System.out.println(s);
//            JSONObject jsonObject = JSONObject.fromObject(s);
//            WeixinUserInfo weixinUserInfo = new WeixinUserInfo();
////            weixinUserInfo
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public static void main(String[] args) {
        String accessToken = MpPush.getAccessToken();
//        MpPush.getUserList(accessToken);
        System.out.println(accessToken);
//        MpPush.getUserInfo(accessToken, "oa4GE0qXVZcsMLZQlXLOHUCrMutI");
    }
}
